/*

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 
    http://www.apache.org/licenses/LICENSE-2.0 
    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License.
      
 */
package jforex.mql;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import jforex.api.IBox;

@SuppressWarnings("serial")
public class MQLAlertDialog extends JDialog implements ActionListener {
    private JPanel myPanel = null;
    private JButton okButton = null;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");

    private DataContainer listModel = null;

    private JLabel frontLabel = new JLabel("");
    private JList jlist = null;

    //private IBox box = null;

    public MQLAlertDialog(IBox box,String title) {
        super((JFrame) null, false);
        //this.box = box;
        //setAlwaysOnTop(true);
        myPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        getContentPane().add(myPanel);
        setTitle(" Alert form "+title);

        JPanel labelPanel = new JPanel(new BorderLayout(10, 10));
        JLabel logoLabel = new JLabel(box.loadIcon("rc/JF_96x96.png"));
        logoLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                Cursor normalCursor = new Cursor(Cursor.HAND_CURSOR);
                setCursor(normalCursor);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
                Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR);
                setCursor(normalCursor);
            }
            @Override
            public void mouseReleased(MouseEvent e) {
               
                super.mouseReleased(e);
                try {
                    Desktop.getDesktop().browse(new URI("http://www.dukascopy.com/jforex/"));
                } catch (Exception e1) {
                }
            }
            @Override
            public void mouseClicked(MouseEvent e) {

                super.mouseClicked(e);
               
            }

        });
        labelPanel.add(logoLabel, BorderLayout.WEST);
        labelPanel.add(frontLabel, BorderLayout.CENTER);
        labelPanel.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 120), 1));
        labelPanel.setPreferredSize(new Dimension(380, 100));
        myPanel.add(labelPanel);

        setIconImage(box.loadImage("com/basystemspark/rc/simple_wobler_16x16.png"));
        //myPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        listModel = new DataContainer();
        jlist = new JList(listModel);
        jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jlist.setVisibleRowCount(5);
        jlist.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                int index = e.getFirstIndex();
                String string = (String) listModel.getElementAt(index);
                string = string.substring(19);
                string = string.replace("\n", "<br>");
                frontLabel.setText("<html><font face=\"Dialog\"><b>" + string + "</b></font></html>");

            }
        });
        JScrollPane scrollPane = new JScrollPane(jlist);
        scrollPane.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 120), 1));
        scrollPane.setPreferredSize(new Dimension(380, 150));
        myPanel.add(scrollPane);
        okButton = new JButton("OK");
        okButton.addActionListener(this);
        myPanel.add(okButton);
        pack();
        int WIDTH = 400;
        int HEIGHT = 320;
        setSize(WIDTH, HEIGHT);
        Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screenDim.width - WIDTH) / 2, (screenDim.height - HEIGHT) / 2);

        //setLocationRelativeTo(frame);

    }

    public void show(long time, String message) {
        listModel.addString(simpleDateFormat.format(new Date(time)) + " | " + message);
        jlist.setSelectionInterval(0, 0);
        String string = message;

        string = string.replace("\n", "<br>");

        frontLabel.setText("<html><font face=\"Dialog\"><b>" + string + "</b></font></html>");
        if (!isVisible()) {
            setVisible(true);
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (okButton == e.getSource()) {

            setVisible(false);
        }

    }

}

class DataContainer implements ListModel {

    private List<String> list = new ArrayList<String>();
    private Set<ListDataListener> listeners = new HashSet<ListDataListener>();

    @Override
    public void addListDataListener(ListDataListener listDataListener) {
        listeners.add(listDataListener);
    }

    @Override
    public Object getElementAt(int index) {
        String rc = list.get(list.size() - 1 - index);

        return rc;
    }

    @Override
    public int getSize() {
        return list.size();
    }

    @Override
    public void removeListDataListener(ListDataListener listDataListener) {
        listeners.remove(listDataListener);
    }

    public void addString(String string) {
        list.add(string);
        for (ListDataListener dataListener : listeners) {
            dataListener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, list.size()));
        }
    }

}
